package com.us.usws.site.ws.entity.channel;

import java.util.ArrayList;
import java.util.List;

import com.us.usws.biz.WSResourceURIReplacer;
import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.channel.Channel;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.flat.channel.DOChannel;
import com.us.usws.service.ro.VideoROService;
import com.us.usws.site.ws.entity.WSEntity;
import com.us.usws.site.ws.entity.WSEntityFactory;
import com.us.usws.site.ws.entity.WSEntityRecords;
import com.us.usws.site.ws.entity.channel.level2studentchannel.WSLevel2StudentChannel;
import com.us.usws.site.ws.entity.channel.studentchannel.WSStudentChannel;
import com.us.usws.site.ws.entity.channel.subchannel.WSSubChannel;
import com.us.usws.site.ws.entity.channel.video.WSChannelVideoDetail;
import com.us.usws.site.ws.entity.channel.video.WSChannelVideos;
import com.us.usws.support.uitls.ServiceFactory;
import com.us.usws.support.uitls.SimpleDataPageUtil;

public class WSChannelEntityPackGenerator {

	//private static Log log = LogFactory.getFactory().getInstance(WSEntityPackGenerator.class);

	private static WSChannelEntityPackGenerator instance = new WSChannelEntityPackGenerator();

	//private BeanConverter bc = BeanConverter.getDefaultConverter();

	private WSChannelEntityPackGenerator() {
	}

	public static WSChannelEntityPackGenerator getInstance() {
		return instance;
	}

	/*---------------- public methods -----------------*/

	/**
	 * get pack for sub channels
	 * @param channels
	 * @return
	 */
	public WSEntity getSubChannelsPack(List<Channel> channels, String focusChannelUid) {
		if (channels == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}
		//prepare records
		List<WSSubChannel> records = new ArrayList<WSSubChannel>(channels.size());

		//loop for channels
		for (Channel eachCh : channels) {

			//gen WS entity
			WSSubChannel oneCh = new WSSubChannel(eachCh);

			//set non-auto fields
			oneCh.setPicture(WSResourceURIReplacer.getChannelPictureURL(eachCh.getUid(), eachCh.getPicture()));

			if (eachCh.getChannelTypeDsid() == SiteConstants.DSID_CHANNEL_TYPE_MEETING) {
				oneCh.setUrl(WSResourceURIReplacer.getChannelMeetingsURL(eachCh.getUid()));
			} else {
				oneCh.setUrl(WSResourceURIReplacer.getChannelVideosListURL(eachCh.getUid()));
			}

			oneCh.setVideoCnt(eachCh.getVideos() == null ? 0 : eachCh.getVideos().size());
			oneCh.setFocused(eachCh.getUid().equals(focusChannelUid) ? true : false);

			//add itmes
			records.add(oneCh);
		}

		//pack records
		WSEntityRecords data = new WSEntityRecords();
		data.setRecords(records);

		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}

	/**
	 * get channel videos
	 * @param results
	 * @return
	 */
	public WSEntity getChannelVideosPack(String channelUid, List<Video> videos, SimpleDataPageUtil pu) {
		if (videos == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}

		//prepare return videos
		WSChannelVideos data = new WSChannelVideos(pu.getPageSize(), pu.getPageNo(), pu.getRowCount(), pu.getPageSize());
		data.setPrevPageUrl(WSResourceURIReplacer.getChannelVideosListURLWithPageNo(channelUid, pu.getPageNo() - 1, pu.getPageCount()));
		data.setNextPageUrl(WSResourceURIReplacer.getChannelVideosListURLWithPageNo(channelUid, pu.getPageNo() + 1, pu.getPageCount()));

		List<WSChannelVideoDetail> records = new ArrayList<WSChannelVideoDetail>(videos.size());
		//loop for channels
		for (Video eachV : videos) {
			//convert to WS entity
			WSChannelVideoDetail vd = PackEntityConverter.genWSChannelVideoDetail(eachV);

			//add one channel to data
			records.add(vd);
		}

		data.setRecords(records);

		//pack records
		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}

	/**
	 * get student channels
	 * @param channels
	 * @return
	 */
	public WSEntity getStudentChannels(List<Channel> channels) {
		if (channels == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}
		//prepare records
		List<WSStudentChannel> records = new ArrayList<WSStudentChannel>(channels.size());

		//loop for channels
		for (DOChannel eachCh : channels) {

			//gen WS entity
			WSStudentChannel oneCh = new WSStudentChannel(eachCh);

			//set non-auto fields
			oneCh.setPicture(WSResourceURIReplacer.getChannelPictureURL(eachCh.getUid(), eachCh.getPicture()));
			if (eachCh.getChannelTypeDsid() == SiteConstants.DSID_CHANNEL_TYPE_MEETING) {
				oneCh.setUrl(WSResourceURIReplacer.getChannelMeetingsURL(eachCh.getUid()));
			} else {
				oneCh.setUrl(WSResourceURIReplacer.getChannelVideosListURL(eachCh.getUid()));
			}
			oneCh.setSubChannelsUrl(WSResourceURIReplacer.getStudentSubChannelsURL(eachCh.getUid()));

			//add itmes
			records.add(oneCh);
		}

		//pack records
		WSEntityRecords data = new WSEntityRecords();
		data.setRecords(records);

		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}

	/**
	 * get level 2 student channels
	 * @param channels
	 * @return
	 */
	public WSEntity getLevel2StudentChannels(List<Channel> channels) {
		if (channels == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}
		//prepare records
		List<WSLevel2StudentChannel> records = new ArrayList<WSLevel2StudentChannel>(channels.size());

		VideoROService vS = ServiceFactory.getInstance().getService(VideoROService.class);
		//loop for channels
		int idx = 0;
		for (DOChannel eachCh : channels) {

			//gen WS entity
			WSLevel2StudentChannel oneCh = new WSLevel2StudentChannel(eachCh);
			int videoCnt = vS.getChannelVideoCount(eachCh.getUid());
			oneCh.setVideoCnt(videoCnt);
			if (idx == 0) {
				oneCh.setFocused(true);
			}
			//add itmes
			records.add(oneCh);
			idx++;
		}

		//pack records
		WSEntityRecords data = new WSEntityRecords();
		data.setRecords(records);

		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}

}
